<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Order;
use App\Model\Order_detail;

/**
 * [fanzekai]
 *
 */
class DetailController extends Controller
{
	/**
	 *[index 后台订单页面]
	 *
	 */
	public function index()
	{
		$pay = [0=>'未付款',1=>'已付款', 2=>'退款中'];
		$order = [0=>'进行中',1=>'已完成', 2=>'已取消'];
		$logistics = [0=>'未发货',1=>'待揽收', 2=>'已发货'];
		$detail= Order::select('*')->paginate(2);

		return view('Admin/Detail/index',['detail' => $detail, 'pay_state' => $pay, 'order_state' => $order,'logistics' => $logistics]);
	}
	/**
	 * [detail 后台订单详情页面]
	 */
	public function detail()
	{
		$pay = [0=>'未付款',1=>'已付款', 2=>'退款中'];
		$order = [0=>'进行中',1=>'已完成', 2=>'已取消'];
		$detail= Order_detail::join('orders','order_detail.oid','=','orders.id')
						->select('*')
						->get();

		return view('Admin/Detail/detail', ['Order_detail' => $detail, 'pay_state' => $pay, 'order_state' => $order]);
	}
	/**
	 *[change 加载修改订单信息页面]
	 */
	 public function change()
 	{

 		$detail= Order::select('*')->get();

 		return view('Admin/Detail/change', ['detail' => $detail,]);
 	}


	/**
	 * [shipments 订单发货处理页面]
	 */
	 public function shipments(Request $request)
	 {
			 $id = $request->id;
			//  dd($id);
		 	 $shipments = Order::where('id',$id)
			 										->update(['logistics' => 2]);
			// dd($shipments);
			if (!$shipments) {
					return response()->json([
							'code' => 403,
							'msg' => '发货失败请重试',
							]);
			}
			if ($shipments == 1) {
					return response()->json([
							'code' => 200,
							'msg' => '发货成功',
							]);
			}
	 }

   /**
    * [refund 退款处理]
    */
	 public function refund(Request $request)
	 {
		 $id = $request->id;
		 $refund = Order::where('id',$id)
												->update(['order_state' => 2]);
			// dd($shipments);
			if (!$refund) {
					return response()->json([
							'code' => 403,
							'msg' => '退款失败',
							]);
			}else {
					return response()->json([
							'code' => 200,
							'msg' => '退款成功',
							]);
			}
	 }
   /**
    * [dochange 修改发货信息处理]
    */
	 public	function dochange(Request $request) # Request $request
	 {
		 $man = $request->input('man');
		 $phone = $request->input('phone');
		 $address = $request->input('address');
		 $money = $request->input('money');
		 $id = $request->input('order_id');

	 	if(!$money){
				 $mod = Order::where('id',$id)->update(['receiving_man'=>$man,'receiving_address'=>$address,'receiving_phone'=>$phone]);
		}else{
			 $mod = Order::where('id',$id)->update(['receiving_man'=>$man,'receiving_address'=>$address,'receiving_phone'=>$phone,'total_money'=>$money]);
		}
		//
		if (!$mod) {

				return response()->json([
					'code'=>1403,
					'msg'=>'修改失败'
				]);
		}else{
			 return response()->json([
				 'code'=>2000,
				 'msg'=>'修改成功'
			 ]);
		}
	 }
}
